﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using interop;

namespace leodesigner
{
    public partial class designer : Form
    {
        public designer()
        {
            InitializeComponent();
            startToolStripMenuItem.Text = "Start Game";
            Designer = this;
//			(new SplashScreen()).Show();
//			splash.Show();
//            System.Threading.Thread.Sleep(2000);
  //          splash.Close();
    //        splash = null;


            this.Show();
            Application.DoEvents();
            main_renderview = new RenderView();
            main_renderview.Show(dockPanel);
            scenetreeview = new SceneTreeView();
            scenetreeview.Show(dockPanel,WeifenLuo.WinFormsUI.Docking.DockState.DockRight);

            propertyview = new PropertyView();
            propertyview.Show(dockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockRight);
            propertyview.DockTo(scenetreeview.Pane, DockStyle.Bottom,-1);

            logview = new LogView();
            logview.Show(dockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockRight);
            logview.DockTo(propertyview.Pane, DockStyle.Fill, -1);

            interop.server.init_system(main_renderview.RenderPanel.Handle, this.Handle);
            openScene();
            bool renderview_focused = true;
            Panel RenderPanel=main_renderview.RenderPanel;

            gamemode = gamemode_stopped;
            while (this.Created)
            {
                if (RenderPanel.Parent.Focused != renderview_focused)
                {
                    renderview_focused = RenderPanel.Parent.Focused;
//                    interop.server.enable_mouse_and_keyboard(renderview_focused);
                }
                if (gamemode==gamemode_running || gamemode==gamemode_step)
                    interop.server.update_system();

                interop.server.render_system(gamemode==gamemode_stopped);
                if (gamemode == gamemode_step)
                    gamemode = gamemode_paused;
                logview.SetTextFromLog();
                Application.DoEvents();
            }
            interop.server.exit_system();
        }

        public void selectionChanged()
        {
//            MyPropertyTable t;
        }


        void openScene()
        {
			 OpenFileDialog pOFD = new OpenFileDialog();

			 pOFD.InitialDirectory=System.IO.Directory.GetCurrentDirectory()+"\\scenes";
			 pOFD.Filter = "Scene Files (*.scn)|*.scn";
			 pOFD.DefaultExt = "scn";
			 pOFD.RestoreDirectory = true;
			 pOFD.Multiselect=false;
			 pOFD.Title="Open Scene File";

			 System.Windows.Forms.DialogResult nResult = pOFD.ShowDialog();

			 if (nResult == System.Windows.Forms.DialogResult.OK)
			 {
				 System.String[] rNames = pOFD.FileNames;
				 String pFileName = rNames[0];
				 pFileName =(System.IO.Path.GetDirectoryName(pFileName )+"\\"+System.IO.Path.GetFileNameWithoutExtension(pFileName ));

				 interop.server.load_scene(pFileName);
                 scenetreeview.Tree.SelectedNodes.Clear();
                 interop.server.fill_treeview(scenetreeview.Tree.Nodes);
                 if (scenetreeview.Tree.Nodes.Count!=0)
                     scenetreeview.Tree.SelectedNodes.Add(scenetreeview.Tree.Nodes[0]);
             }

        }

		 System.Windows.Forms.DialogResult SaveSceneAs()
		 {
			 SaveFileDialog pOFD = new SaveFileDialog();

			 String Cwd=System.IO.Directory.GetCurrentDirectory();
			 pOFD.InitialDirectory=Cwd+"\\scenes";
			 pOFD.Filter = "Scene Files (*.scn)|*.scn";
			 pOFD.DefaultExt = "scn";
			 pOFD.RestoreDirectory=true;
			 pOFD.Title="Save Scene File As...";

			 System.Windows.Forms.DialogResult res=pOFD.ShowDialog();
			 if (res==System.Windows.Forms.DialogResult.OK)
			 {
				 string[] rNames = pOFD.FileNames;
				 string pFileName = rNames[0];

                 interop.server.save_scene(pFileName);
//				 g_game.act_scene.save(g_game.act_scene,ch);
//				 format_scene(ch);
				 pFileName =(System.IO.Path.GetDirectoryName(pFileName )+"\\"+System.IO.Path.GetFileNameWithoutExtension(pFileName ));
				 pFileName=pFileName.ToLower();
				 string cwd=System.IO.Directory.GetCurrentDirectory() + "\\";
				 cwd=cwd.ToLower();
				 int i=pFileName.IndexOf(cwd);

                 string FileName;
				 if (i==0)
					 FileName=pFileName.Substring(cwd.Length);
				 else
					 FileName=pFileName;

//				 sceneTreeView.FillSceneTree();

				 this.Text="Leo Designer - " + FileName;
//				 this.changed=false;
			 }

			 return res;
		 }


        public RenderView main_renderview;
        public SceneTreeView scenetreeview;
        public PropertyView propertyview;
        public LogView logview;
        public static designer Designer=null;

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openScene();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveSceneAs();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void reloadScriptsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gamemode != gamemode_stopped)
            {
                MessageBox.Show("Csak lestoppolva toltsd ujra a scripteket lecci", "Script Reload");
                return;
            }
            interop.server.reload_scripts();
        }


        const int gamemode_running = 0;
        const int gamemode_stopped = 1;
        const int gamemode_paused = 2;
        const int gamemode_step = 3;

        int gamemode;

        public void play()
        {
            gamemode = gamemode_running;
            interop.server.init_scene();
        }

        public void stop()
        {
            gamemode = gamemode_stopped;
            interop.server.exit_scene();
        }

        public void pause()
        {
            if (gamemode == gamemode_running)
                gamemode = gamemode_paused;
            else if (gamemode == gamemode_paused)
                gamemode = gamemode_step;
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            play();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stop();
        }

        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pause();
        }

        private void textDrawEnabledToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interop.server.debugtexts_enabled(textDrawEnabledToolStripMenuItem.Checked);
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interop.server.new_scene();
            scenetreeview.Tree.SelectedNodes.Clear();
            interop.server.fill_treeview(scenetreeview.Tree.Nodes);
            if (scenetreeview.Tree.Nodes.Count != 0)
                scenetreeview.Tree.SelectedNodes.Add(scenetreeview.Tree.Nodes[0]);

        }

        

    }
}
